package open

import (
	"context"
	"sale-admin/internal/app/web/service/statistics"
	"sale-admin/internal/dao"
	"strconv"
	"strings"

	"gorm.io/gorm"
)

type Open struct{}

// 历史数据列表
func (o Open) GetHistory(ctx context.Context, db *gorm.DB, RobotIds, StartTime, EndTime string, Page, Limit int) (data interface{}, totalCount statistics.OrderDescTotalCount, count int64, err error) {
	// 转换robot_name 和 sales_id
	robotList, _, _ := dao.Robot{}.RobotList(ctx, db, "", "", RobotIds, "", "", "", "", "")
	var SalesIDList = []string{}
	for _, v := range robotList {
		SalesIDList = append(SalesIDList, v.SalesID)
	}
	salesID := strings.Join(SalesIDList, ",")
	// 获取时间范围内的订单相关数据
	data, count, totalCount, err = statistics.OrderStatisticService{}.OrderDesc(ctx, db, "", Page, Limit,
		StartTime, EndTime, salesID, "", "", "", "", []string{})
	totalCount.TotalMyDiscountAmount = totalCount.TotalDisCount
	return data, totalCount, count, err
}

// 历史数据列表--展开分组
func (o Open) GetHistoryGroup(ctx context.Context, db *gorm.DB, RobotIds, StartTime, EndTime string, Page, Limit int) (data interface{}, totalRobotCount int64, totalCountData interface{}, err error) {

	// 机器展开分组 - 机器的 实时数据
	data, totalRobotCount, totalCountData, err = statistics.StatisticsService{}.ListOpen(
		ctx, db, "", "", 1, StartTime, EndTime, Page, Limit, RobotIds)
	return data, totalRobotCount, totalCountData, err
}

// GetTodayTopGoods 获取今日的商品排行榜
func (o Open) GetTodayTopGoods(ctx context.Context, db *gorm.DB, RobotIds, StartTime, EndTime string) (list []statistics.GoodsSoldCountReturn, totalAmount float64, totalSoldCount int64, count int64, err error) {
	list, totalAmount, totalSoldCount, count, err = statistics.OrderStatisticService{}.GoodsTransactionAmountDescendingList(ctx, db, "", RobotIds, "", "", "", "", []string{}, StartTime, EndTime, 1, 100)
	return list, totalAmount, totalSoldCount, count, err
}

// 客户画像
func (o Open) GetTodayDraw(ctx context.Context, db *gorm.DB, RobotIds, StartTime, EndTime, DrawType string) (data interface{}, err error) {
	DrawTyp1, _ := strconv.Atoi(DrawType)

	robotList, _, _ := dao.Robot{}.RobotList(ctx, db, "", "", RobotIds, "", "", "", "", "")
	var SalesIDList = []string{}
	for _, v := range robotList {
		SalesIDList = append(SalesIDList, v.SalesID)
	}
	salesID := strings.Join(SalesIDList, ",")
	data, err = statistics.DrawService{}.Draw(ctx, db, "", "",
		0, salesID, []string{}, DrawTyp1, StartTime, EndTime, "", "", "", 1, 100)

	return data, err
}
